package com.zishuimuyu;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.Setting;
import cn.hutool.system.SystemUtil;
import com.zishuimuyu.utills.MyFileTypeUtil;
import com.jcraft.jsch.ChannelSftp;
import com.zishuimuyu.utills.SftpUtil;
import org.apache.commons.configuration2.INIConfiguration;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.ini4j.Ini;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author：GJH
 * @date 2022年02月14日
 * @company：北京木联能软件股份有限公司
 */
public class test {
    @Test
    public void test1() throws Exception {
        try {
            ChannelSftp sftp = SftpUtil.sftp("192.168.3.139", "root", "mlnsoft", 22, "GBK", null, null);
            Vector ls = sftp.ls("/PVFSDataJiKong");


            // List<String> fileNames = SftpUtil.sshSftpDownload("192.168.3.139", "root", "mlnsoft", 22, "GBK","/PVFSDataJiKong","D:\\mlnsoft\\sftpDownloadAndUpload\\WPFSData",  null,false);
            // SftpUtil.sftpDownload("192.168.3.139", "root", "mlnsoft", 22, "GBK","/PVFSDataJiKong","D:\\mlnsoft\\sftpDownloadAndUpload\\WPFSData",  "XNFCFD_20211013_1710_FJ.PVD",false);
            List<String> fileNames = SftpUtil.sshSftpUpload("192.168.3.139", "root", "mlnsoft", 22, "GBK", "D:\\mlnsoft\\sftpDownloadAndUpload\\WPFSData", "/PVFSDataJiKong", null);
            for (String fileName : fileNames) {
                System.out.println("-----" + fileName);
                System.out.println("-----" + StrUtil.str(StrUtil.byteBuffer(fileName, "GBK"), "UTF-8"));
                // System.out.println("-----"+fileName);
                // StrUtil.byteBuffer(fileName,"GBK");
                // fileNames2.add(gbk);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // FtpConfig ftpConfig = new FtpConfig("47.97.25.163", 22, "root", "Guojunhua1991", CharsetUtil.charset("UTF-8"));
        // ftpConfig.setConnectionTimeout(1500);
        // ftpConfig.setSoTimeout(1500);
        // Sftp sftp = new Sftp(ftpConfig);
        //
        // boolean upload = sftp.upload("/www/test", FileUtil.file("E:\\back\\WPFSData\\BLFD01_20210525_1945_CFT.WPD"));
        // ChannelSftp sftp = SftpUtil.sshSftpDownload();
        // SFTPUtil.download("/PVFSDataJiKong",);
        // SFTPUtil.disconnect(sftp);
        // Vector ls = sftp.ls("/PVFSDataJiKong");
        // for (Object l : ls) {
        //     System.out.println(l);
        // }
        // // SftpUtil.downloadFile("192.168.3.139","22","root","mlnsoft","/PVFSDataJiKong","");
        // SftpUtil.disconnect(sftp);
    }

    @Test
    public void test2() throws Exception {
        File iniFile = FileUtil.file("E:\\back\\uploadFileCount", "2022-02-14.ini");
        Ini ini = new Ini(iniFile);
        Set<String> cft = ini.get("CFT").keySet();
        //加载文件
        List<String> fileNames = new ArrayList<>();
        fileNames.add("22222");
        fileNames.add("BLFD01_20210525_1945_CFT.WPD");
        //获取已经下载的
        Set<String> downloadedFileNames = ini.get("CFT").keySet();
        //筛选出没有下载的文件名
        fileNames = fileNames.stream().filter(fileName -> !downloadedFileNames.contains(fileName)).collect(Collectors.toList());
        for (String fileName : fileNames) {
            System.out.println(fileName);
        }
        // System.out.println(iniFile.getAbsolutePath());
        // String s = "D:\\mlnsoft\\sftpDownloadAndUpload\\WPFSDataBak" + "\\" + DateUtil.today().substring(0, 7).replace("-", "");
        // System.out.println(s);
        // FileUtil.mkdir(s);

    }

    @Test
    public void test3() throws IOException {
        String path = "E:\\back\\wddata";
        File file = FileUtil.file(path, "hunyuan-18-20230202.txt");
        System.out.println(file.getName());
        System.out.println(file.getParent());
        System.out.println(file.getAbsolutePath());
        System.out.println(FileUtil.getPrefix(file));
        MyFileTypeUtil.transToEFileForWeatherFile(file, "E:\\back\\wddata");
        String property = System.getProperty(SystemUtil.LINE_SEPARATOR);
        System.out.println(System.getProperty(SystemUtil.LINE_SEPARATOR));
    }

    @Test
    public void test4() throws Exception {
        Setting setting = new Setting("D:\\mlnsoft\\downloadFileCount\\downloadedFileNames.ini", true);
        System.out.println(setting.get("2024-03-28", "intang_2024032806_power.csv"));
        System.out.println(setting.containsKey("jintang-06-20240328.txt"));
        //setting.putByGroup(DateUtil.formatDate(DateUtil.parse(StrUtil.subBetween("intang_2024032806_power.csv", "_"), "yyyyMMddHH")))
    }

    //解析ini文件
    @Test
    public void parseIniFile()  {
        // 创建配置构建器
        // 加载INI文件
        String path = "D:\\mlnsoft\\downloadFileCount\\downloadedFileNames.ini";
        INIConfiguration ini=null;
        try {
             ini = new Configurations().ini(path);
        } catch (ConfigurationException e) {
            throw new RuntimeException(e);
        }
        // 读取 database 部分的配置
        Iterator<String> keys = ini.getKeys();
        keys.forEachRemaining(
                key -> {
                    System.out.println(key);
                }
        );
        boolean b = ini.containsKey("jintang-18-20240327.txt");
        System.out.println(b);
        // 打印配置信息
    }
}

